package com.hhxy.book.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hhxy.MD5;
import com.hhxy.R;
import com.hhxy.book.entity.User;
import com.hhxy.book.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import sun.nio.cs.US_ASCII;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 一粒尘埃
 * @since 2023-03-04
 */
@RestController
@RequestMapping("/book/user")
public class UserController {

    @Autowired
    private UserService userService;

    //管理员列表
    @PostMapping("Adminlist/{current}/{limit}")
    public R Adminlist(@PathVariable long current,
                       @PathVariable long limit,
                       @RequestBody User user) {

        Page<User> page = new Page<>(current,limit);
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_role",1);
        if (!StringUtils.isEmpty(user.getUserName())) {
            queryWrapper.like("user_name",user.getUserName());
        }
        if (!StringUtils.isEmpty(user.getUserNumber())) {
            queryWrapper.like("user_name",user.getUserNumber());
        }

        userService.page(page,queryWrapper);
        long total = page.getTotal();
        List<User> records = page.getRecords();

        return R.ok().data("total",total).data("data",records);
    }

    //用户列表
    @PostMapping("Userlist/{current}/{limit}")
    public R Userlist(@PathVariable long current,
                       @PathVariable long limit,
                       @RequestBody User user) {

        Page<User> page = new Page<>(current,limit);
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_role",2);
        if (!StringUtils.isEmpty(user.getUserName())) {
            queryWrapper.like("user_name",user.getUserName());
        }
        if (!StringUtils.isEmpty(user.getUserNumber())) {
            queryWrapper.like("user_name",user.getUserNumber());
        }

        userService.page(page,queryWrapper);
        long total = page.getTotal();
        List<User> records = page.getRecords();

        return R.ok().data("total",total).data("data",records);
    }

    //用户登录
    @PostMapping("login")
    public R login(@RequestBody User user) {
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_number",user.getUserNumber());
        User one = userService.getOne(queryWrapper);
        if (one == null) {
            return R.error().message("该用户不存在，请重试或注册账号!");
        }else {
            if (!one.getUserPassword().equals(user.getUserPassword())) {
                return R.error().message("密码错误，请重试！");
            }else if (one.getStatus() == 1) {
                return R.error().message(("抱歉，该用户已被禁用!"));
            }
            return R.ok().data("data",one);
        }
    }

    //用户注册
    @PostMapping("addUser")
    public R register(@RequestBody User user) {
        user.setUserRole(2);
        user.setUserPassword(MD5.encrypt(user.getUserPassword()));
        userService.save(user);
        return R.ok();
    }

    //添加管理员
    @PostMapping("addAdmin")
    public R registerAdmin(@RequestBody User user) {
        user.setUserRole(1);
        user.setUserPassword(MD5.encrypt(user.getUserPassword()));
        userService.save(user);
        return R.ok();
    }

    //删除用户
    @DeleteMapping("{id}")
    public R delete(@PathVariable String id) {
        userService.removeById(id);
        return R.ok();
    }

    //批量删除
    @DeleteMapping("deleteBatch")
    public R deleteBatch(@RequestBody List<String> list) {
        userService.removeByIds(list);
        return R.ok();
    }

    //id查询
    @GetMapping("getById/{id}")
    public R getById(@PathVariable String id) {
        User user = userService.getById(id);
        return R.ok().data("data",user);
    }

    //修改
    @PutMapping("update")
    public R update(@RequestBody User user) {
        userService.updateById(user);
        return R.ok();
    }

}

